<?php
namespace Admin\Controller;
use User\Api\UserApi as UserApi;
class PriceController extends AdminController {
    public function index(){
        $nickname       =   I('nickname');
        $map['status']  =   array('egt',0);
        if(is_numeric($nickname)){
            $map['uid|nickname']=   array(intval($nickname),array('like','%'.$nickname.'%'),'_multi'=>true);
        }else{
            $map['nickname']    =   array('like', '%'.(string)$nickname.'%');
        }
        $list   = $this->lists('price', $map);
          foreach ($list as $k => $val) {
                  $goods = M('shop')->where(array('id'=>$val['pid']))->find();
                  $where['id'] = array('in',$val['ar']);
                  $attr = M('att')->where($where)->field('id,title')->select();
                  $strval = '';   
              foreach($attr as $value){ 
                  $strval .= $value['title'].'、';
                  $strval = rtrim($strval,'、');
                  $attrval = $strval;
                  $list[$k]['gname'] = $goods['title'];
                  $list[$k]['attrval'] = $attrval;
         }
       }
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '用户信息';
        $this->display();
      }
    public function add(){
        if(IS_POST){
            $price = D('price');
            foreach (I('post.mprice') as $k => $v) {
               $date['pid']=I('post.pid')[$k];
               $date['mprice']=$v;
               $date['sprice']=I('post.sprice')[$k];
               $date['ar']=I('post.ar')[$k];
               $date['num']=I('post.num')[$k];
               $res= $price->create();        
        if ($res) {
        if ($price->add($date)) {
            $this->success('添加成功',U('Shopatt/index'));
        }else{
            $this->error('添加失败',U('Shopatt/index'));
        }
        }else{
            $this->error(M('att')->getError());
        }
     }
        }else{
            $id=I('get.id');
            $sev = M("att")->where('gid='.$id)->select();
            $arr=[];
            foreach ($sev as $v) {
            $arr[$v['pid']][]=$v['title'];
            $ar[$v['pid']][]=$v['id'];
         }
        $a=$this->dikaer(array_values($arr));
        $b=$this->dikae(array_values($ar));
        foreach ($b as $v) {
          $arr = explode(',', $v);
         asort($arr);
         $attrvid[] = implode(',', $arr);
        }
        $this->assign('pid',$id);
        $this->assign('ar',$attrvid);
        $this->assign('arr',$a);
        $this->display();
        }
    }
    public function dikaer($arr){  
      $arr1 = array();  
	    $result = array_shift($arr);  
	    while($arr2 = array_shift($arr)){  
	    $arr1 = $result;  
	    $result = array();  
	    foreach($arr1 as $v){  
      foreach($arr2 as $v2){  
      if(!is_array($v))$v = array($v);  
      if(!is_array($v2))$v2 = array($v2);  
      $result[] = array_merge_recursive($v,$v2);  
      }  
    }  
  }  
        return $result;  
}  
    function dikae($arr){  
        $arr1 = array();  
        $result = array_shift($arr);  
        while($arr2 = array_shift($arr)){  
        $arr1 = $result;  
        $result = array();  
        foreach($arr1 as $v){  
        foreach($arr2 as $v2){  
        $result[] = $v.','.$v2;  
      }  
    }  
  }  
  return $result;  
} 
    public function deletes(){
        $id=I('get.id');
        $res=M('price')->where("id=$id")->delete($_POST);
        if ($res) {
            $this->success('删除成功',U('index'));
        }else{
            $this->error('删除失败',U('index'));
    }
   } 

   public function update(){
            if (IS_POST) {
            $id=I('post.id');
            $res=M('price')->where('id='.$id)->save($_POST);
            if ($res) {
                $this->success('修改成功',U('index'));
            }else{
                $this->error('修改失败',U('update?id='.$id));
            }
        }else{
             $id=I('get.id');
             $User = M("price")->find("$id");// 实例化User对象
             $this->assign('User',$User);
             $this->display();
        }
   }  
    public function setStatus($model='Price'){
        return parent::setStatus($model);
    }

    public function changeStatus($method=null){
        $id = array_unique((array)I('id',0));
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] =   array('in',$id);
        switch ( strtolower($method) ){
            case 'forbiduser':
                $this->forbid('price', $map );
                break;
            case 'resumeuser':
                $this->resume('price', $map );
                break;
            case 'deleteuser':
                $this->delete('price', $map );
                break;
            default:
                $this->error('参数非法');
        }
    }
}